package edu.scause.gs.control;

import java.text.ParseException;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.scause.gs.model.GoodsInfo;
import edu.scause.gs.model.Orders;
import edu.scause.gs.service.AdminService;

public class AdminAction extends ActionSupport {

	/**
	 * @return
	 */
	private AdminService adminService;
	private String adminid;
	private String adminpassword;
	private GoodsInfo goodsInfo;
	private List<GoodsInfo> goodsInfolist;
	private String goodsId;
	private List<Orders> confirmlist;
	private int OrderId;
	
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public String getAdminid() {
		return adminid;
	}

	public void setAdminid(String adminid) {
		this.adminid = adminid;
	}

	public String getAdminpassword() {
		return adminpassword;
	}

	public void setAdminpassword(String adminpassword) {
		this.adminpassword = adminpassword;
	}

	public GoodsInfo getGoodsInfo() {
		return goodsInfo;
	}

	public void setGoodsInfo(GoodsInfo goodsInfo) {
		this.goodsInfo = goodsInfo;
	}

	public List<GoodsInfo> getGoodsInfolist() {
		return goodsInfolist;
	}

	public void setGoodsInfolist(List<GoodsInfo> goodsInfolist) {
		this.goodsInfolist = goodsInfolist;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public List<Orders> getConfirmlist() {
		return confirmlist;
	}

	public void setConfirmlist(List<Orders> confirmlist) {
		this.confirmlist = confirmlist;
	}

	public int getOrderId() {
		return OrderId;
	}

	public void setOrderId(int orderId) {
		OrderId = orderId;
	}

	public String AdminLogin() {
		// TODO Auto-generated method stub
		if (adminService.AdminsLogin(adminid, adminpassword)) {
			ActionContext actionContext = ActionContext.getContext();
			actionContext.getSession().put("AdminID", adminid);
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String AddGoods() {
		if (adminService.AddGoodsInfo(goodsInfo)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String ModifyGoods() {
		if (adminService.ModifyGoodsInfo(goodsInfo)) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String DisplayGoods() {
		setGoodsInfolist(adminService.DisplayGoods());
		if (! goodsInfolist.isEmpty()) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String DetailGoods() {
		System.out.println(goodsId);
		setGoodsInfo(adminService.FindDetail(goodsId));
		if (goodsInfo != null) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String DeleteGoods() {
		if (adminService.DeleteGoodsInfo(goodsId)) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String ManageOrders() throws ParseException  {
		setConfirmlist(adminService.ManageClientOrders());
		if (confirmlist != null) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	public String TerminateOrders () {
		ActionContext actionContext = ActionContext.getContext();
		String tempadminid = (String) actionContext.getSession().get("AdminID");
		System.out.println(tempadminid);
		if (adminService.TerminateClientsOrder(OrderId, tempadminid)) {
			return SUCCESS;
		}
		return ERROR;
	}
}